package com.itcam.list_.map_;


import com.itcam.list_.map_.domain.Person;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.TreeMap;

/**
 * Author:Cammy.Wu
 * Date:2024-06-05
 * Description:
 */

public class MapDemo02 {
    /**
     * 双列集合底层的数据结构，都是针对键有效，值没有关系
     * <p>
     * HashMap：键唯一（重写equals和hashCode方法）
     * TreeMap：键排序
     * LinkedHashMap：键唯一，且可以保证存取顺序
     */
    public static void main(String[] args) {
        showHashMap();
        showTreeMap();
        showLinkedHashMap();
    }

    private static void showHashMap() {
        HashMap<Person, String> hashMap = new HashMap<>();
        hashMap.put(new Person("cammy", 23), "汕尾");
        hashMap.put(new Person("jerry", 20), "广州");
        hashMap.put(new Person("tim", 33), "广州");
        hashMap.put(new Person("million", 23), "深圳");
        System.out.println("hashMap = " + hashMap);
    }

    private static void showTreeMap() {
        TreeMap<Person, String> treeMap = new TreeMap<>();
        treeMap.put(new Person("cammy", 23), "汕尾");
        treeMap.put(new Person("jerry", 20), "广州");
        treeMap.put(new Person("tim", 33), "广州");
        treeMap.put(new Person("million", 23), "深圳");
        System.out.println("treeMap = " + treeMap);
    }

    private static void showLinkedHashMap(){
        LinkedHashMap<Person, String> linkedHashMap = new LinkedHashMap<>();
        linkedHashMap.put(new Person("cammy", 23), "汕尾");
        linkedHashMap.put(new Person("jerry", 20), "广州");
        linkedHashMap.put(new Person("tim", 33), "广州");
        linkedHashMap.put(new Person("million", 23), "深圳");
        System.out.println("linkedHashMap = " + linkedHashMap);
    }
}
